# -*- coding: UTF-8 -*-
"""
@Time : 2022/12/5
@Author : Gloria
"""
from django.contrib import admin
from django.utils.safestring import mark_safe
from app01.models import Articles
from app01.templatetags.my_filter import translate_time

# 文章表
class ArticleAdmin(admin.ModelAdmin):

    def get_cover(self: Articles):
        if self.cover:
            return mark_safe(
                f'<a href="/article/{self.nid}/" target="_blank"><img src="{self.cover.url.url}" style="height: 60px; border-radius: 5px"></a>')
        return

    get_cover.short_description = '文章封面'

    def get_tags(self: Articles):
        tag_list = ','.join([tag.title for tag in self.tag.all()])
        return tag_list

    get_tags.short_description = '文章标签'

    # def get_title(self):
    #     return mark_safe(f'<a href="/article/{self.nid}/" target="_blank">{self.title}</a>')

    # get_title.short_description = '文章'

    def get_status(self: Articles):
        print(self)
        # return mark_safe(f"""
        # <i style="background-color: red">{}</i>
        # """)

    get_status.short_description = '文章保存状态'

    def get_edit_delete_btn(self: Articles):
        return mark_safe(f"""
        <a href="/backend/edit_article/{self.nid}/" target="_blank">编辑</a>
        <a href="/admin/app01/articles/{self.nid}/delete/">删除</a>
        """)

    get_edit_delete_btn.short_description = '操作'

    def get_change_date(self: Articles):
        change_date = translate_time(self.change_date)
        return mark_safe(change_date)

    get_change_date.short_description = '文章修改日期'

    list_display = ['title',
                    'author',
                    get_cover,
                    get_tags,
                    'category',
                    'status',
                    'look_count', 'digg_count', 'comment_count', 'collects_count', 'word',
                    get_change_date,
                    get_edit_delete_btn]

    def action_word(self, request, queryset):
        for obj in queryset:
            word = len(obj.content)
            obj.word = word
            obj.save()

    action_word.short_description = '获取文章字数'

    def delete_comment(self, request, queryset):
        for article in queryset:
            article: Articles = article
            article.comment_set.all().delete()
            article.comment_count = 0
            article.save()

    delete_comment.short_description = '删除文章所有评论'

    action_word.type = 'success'

    actions = [action_word, delete_comment]